package com.ce.pms.occupation.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.util.Assert;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.HmEmployeeDomain;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.occupation.dao.IPerfectItemRequisitionDao;
import com.ce.pms.occupation.dao.IPerfectItemTrackDao;
import com.ce.pms.occupation.model.PerfectItemDispatchDO;
import com.ce.pms.occupation.model.PerfectItemRequisitionDO;
import com.ce.pms.occupation.model.PerfectItemTrackDO;
import com.ce.pms.occupation.service.IPerfectItemTrackService;
import com.ce.pms.occupation.vo.PerfectItemTrackVO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;
/**
 * 功能描述: 整改跟进Service实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-12-30
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class PerfectItemTrackServiceImpl extends PmsBaseServiceImpl implements
		IPerfectItemTrackService {
	/**
	 * 方法描述: 分页查询所有整改跟进
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param SearchVO
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryPerfectItemTrackVOByPage(PerfectItemTrackVO searchVO){
		Assert.notNull(searchVO);
		Page page = perfectItemTrackDao.queryPerfectItemTrackDOByPage(searchVO, searchVO.getPerfectItemRequisitionId());
		page.setResult(changelistDOToVO(page.getResult())) ;
		return page;
	}

	/**
	 * 方法描述: 根据ID查询整改跟进VO
	 * @author 胡煦
	 * Create on:2008-12-29
	 * @param id
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemTrackVO queryPerfectItemTrackVOById(Long id){
		Assert.notNull(id,"id isnot Null");
		PerfectItemTrackDO perfectItemTrackDO = perfectItemTrackDao.findById(id);
		return this.assembleD2V(perfectItemTrackDO, null);
	}


	/**
	 * 方法描述: 增加整改跟进方法
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param perfectItemTrackVO
	 * @return PerfectItemTrackVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemTrackVO addPerfectItemTrack(PerfectItemTrackVO perfectItemTrackVO){
		Assert.notNull(perfectItemTrackVO);
		PerfectItemRequisitionDO itemRequisitionDO = this.checkAddIsTrue(perfectItemTrackVO.getPerfectItemRequisitionId());
		perfectItemTrackVO = this.doCompleteYnFor(perfectItemTrackVO, itemRequisitionDO);
		PerfectItemTrackDO perfectItemTrackDO = this.assembleV2D(perfectItemTrackVO, null);
		perfectItemTrackDO = perfectItemTrackDao.saveObject(perfectItemTrackDO);
		perfectItemTrackVO.setKeyword(perfectItemTrackDO.getKeyword());
		return perfectItemTrackVO;
	}
	/**
	 * 方法描述: 新增整改跟进验证方法
	 * 未派工的项目，不能进行整改跟进
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemRequisitionId
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemRequisitionDO checkAddIsTrue(Long perfectItemRequisitionId){
		Assert.notNull(perfectItemRequisitionId,"id isnot Null");
		PerfectItemRequisitionDO perfectItemRequisitionDO = perfectItemRequisitionDao.findById(perfectItemRequisitionId);
		PerfectItemDispatchDO dispatchDO = perfectItemRequisitionDO.getPerfectItemDispatch();
		//已经完工和未派工的项目，不能进行整改跟进
		if(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO.equals(perfectItemRequisitionDO.getDispatchYn())||BlankUtil.isBlank(dispatchDO)){
			throw new BusinessException("occupation:occupation_perfectItemTrack_add_exception", BusinessException.WARNING);
		}
		return perfectItemRequisitionDO;
	}
	/**
	 * 方法描述: 根据状态处理完工状态
	 * @author 胡煦
	 * Create on:2009-1-22
	 * @param perfectItemTrackVO，PerfectItemRequisitionDO
	 * @return PerfectItemTrackVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private PerfectItemTrackVO doCompleteYnFor(PerfectItemTrackVO perfectItemTrackVO,PerfectItemRequisitionDO itemRequisitionDO){
		if(!BlankUtil.isBlank(perfectItemTrackVO.getCompleteYn())){
			if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(perfectItemTrackVO.getCompleteYn())){
				this.checkCompleteIsTrue(perfectItemTrackVO);
				perfectItemTrackVO.setCompleteYn(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
				itemRequisitionDO.setCompleteYn(SysrefcodeDO.CONSTANTS_DIGITAL_ONE);
			}else{
				perfectItemTrackVO.setCompleteYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
				itemRequisitionDO.setCompleteYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
			}
		}else{
			perfectItemTrackVO.setCompleteYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
			itemRequisitionDO.setCompleteYn(SysrefcodeDO.CONSTANTS_DIGITAL_ZERO);
		}
		perfectItemRequisitionDao.saveOrUpdateObject(itemRequisitionDO);
		return perfectItemTrackVO;
	}

	/**
	 * 方法描述: 修改整改跟进方法
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param perfectItemTrackVO
	 * @return PerfectItemTrackVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemTrackVO editPerfectItemTrack(PerfectItemTrackVO perfectItemTrackVO){
		Assert.notNull(perfectItemTrackVO);
		Assert.notNull(perfectItemTrackVO.getKeyword(),"id isnot Null");
		Assert.notNull(perfectItemTrackVO.getPerfectItemRequisitionId(),"id isnot Null");

		PerfectItemTrackDO perfectItemTrackDO = checkEditIsTrue(perfectItemTrackVO.getKeyword());
		PerfectItemRequisitionDO itemRequisitionDO = perfectItemTrackDO.getPerfectItemRequisition();
		//只允许有一笔跟进通过记录
		if(!BlankUtil.isBlank(perfectItemTrackVO.getCompleteYn())){
			if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(perfectItemTrackVO.getCompleteYn())&&
					!SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(perfectItemTrackDO.getCompleteYn())){
				this.checkCompleteIsTrue(perfectItemTrackVO);
			}
		}
		perfectItemTrackVO = this.doCompleteYnFor(perfectItemTrackVO, itemRequisitionDO);
		this.assembleV2D(perfectItemTrackVO, perfectItemTrackDO);
		if(BlankUtil.isBlank(perfectItemTrackVO.getCompleteDate())){
			perfectItemTrackDO.setCompleteDate(null);
		}
		perfectItemTrackDao.updateObject(perfectItemTrackDO);
		return perfectItemTrackVO;
	}
	/**
	 * 方法描述: 修改整改跟进验证方法
	 * 项目已经完工,不能修改除已经完工外的整改跟进
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemTrackId
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemTrackDO checkEditIsTrue(Long perfectItemTrackId){
		Assert.notNull(perfectItemTrackId,"id isnot Null");
		PerfectItemTrackDO perfectItemTrackDO = perfectItemTrackDao.findById(perfectItemTrackId);
		if(BlankUtil.isBlank(perfectItemTrackDO)){
			return null;
		}
		PerfectItemTrackDO itemTrackDO = perfectItemTrackDao.queryCompletePerfectItemTrack(perfectItemTrackDO.getPerfectItemRequisition().getKeyword());
		//项目已经完工,不能修改除已经完工外的整改跟进
		if(!BlankUtil.isBlank(itemTrackDO)&&!SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(perfectItemTrackDO.getCompleteYn())){
			throw new BusinessException("occupation:occupation_perfectItemTrack_edit_exception", BusinessException.WARNING);
		}
		return perfectItemTrackDO;
	}
	/**
	 * 方法描述: 删除整改跟进方法
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param perfectItemTrackId
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean deletePerfectItemTrack(Long perfectItemTrackId){
		Assert.notNull(perfectItemTrackId,"id isnot Null");
		this.checkDeleteIsTrue(perfectItemTrackId);
		try {
			perfectItemTrackDao.deleteObjectById(perfectItemTrackId);
			return true;
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw new BusinessException("occupation:system_delete_inused_exception", BusinessException.WARNING);
		}
	}
	/**
	 * 方法描述: 删除整改跟进验证方法
	 * 已经完工的项目跟进，不能进行删除
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemTrackId
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PerfectItemTrackDO checkDeleteIsTrue(Long perfectItemTrackId){
		Assert.notNull(perfectItemTrackId,"id isnot Null");
		PerfectItemTrackDO perfectItemTrackDO = perfectItemTrackDao.findById(perfectItemTrackId);
		//已经完工的项目跟进，不能进行删除
		if(SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(perfectItemTrackDO.getCompleteYn())){
			throw new BusinessException("occupation:occupation_perfectItemTrack_delete_exception", BusinessException.WARNING);
		}
		return perfectItemTrackDO;
	}
	/**
	 * 方法描述: 整改跟进完工验证方法
	 * 只允许有一笔跟进通过记录
	 * 验证通过的日期（完工日期）必须为最新日期
	 * @author 胡煦
	 * Create on:2008-12-31
	 * @param perfectItemTrackVO
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkCompleteIsTrue(PerfectItemTrackVO perfectItemTrackVO){
		Assert.notNull(perfectItemTrackVO);
		Assert.notNull(perfectItemTrackVO.getPerfectItemRequisitionId(),"id isnot Null");
		//获取某个项目中已经完工项目跟进记录
		PerfectItemTrackDO itemTrackDO = perfectItemTrackDao.queryCompletePerfectItemTrack(perfectItemTrackVO.getPerfectItemRequisitionId());
		//如果存在完工则抛出异常
		if(!BlankUtil.isBlank(itemTrackDO)){
			throw new BusinessException("occupation:occupation_perfectItemTrack_complete_exception", BusinessException.WARNING);
		}
		//获取某个项目中最大跟进日期
		Date date = perfectItemTrackDao.queryMaxDateOfPerfectItemTrack(perfectItemTrackVO.getPerfectItemRequisitionId());
		//验证通过的日期（完工日期）必须为最新日期
		if(!BlankUtil.isBlank(date)){
			if(date.compareTo(perfectItemTrackVO.getCompleteDate()) > 1){
				throw new BusinessException("occupation:occupation_complete_date_exception", BusinessException.WARNING);
			}
		}
	}
	/**
	 * 方法描述: PerfectItemTrackDO to PerfectItemTrackVO
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param PerfectItemTrackDO ， PerfectItemTrackVO
	 * @return PerfectItemTrackVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private PerfectItemTrackVO assembleD2V(PerfectItemTrackDO perfectItemTrackDO,PerfectItemTrackVO perfectItemTrackVO){
		if(BlankUtil.isBlank(perfectItemTrackVO)){
			perfectItemTrackVO = new PerfectItemTrackVO();
		}
		PopulateUtil.populate(perfectItemTrackDO, perfectItemTrackVO);
		perfectItemTrackVO.setTrackerId(perfectItemTrackDO.getTracker()==null?null:perfectItemTrackDO.getTracker().getEmployeeId());
		perfectItemTrackVO.setTrackerName(perfectItemTrackDO.getTracker()==null?null:perfectItemTrackDO.getTracker().getEmployeeName());
		perfectItemTrackVO.setPerfectItemRequisitionId(perfectItemTrackDO.getPerfectItemRequisition()==null?null:perfectItemTrackDO.getPerfectItemRequisition().getKeyword());
		return perfectItemTrackVO;
	}
	/**
	 * 方法描述:PerfectItemTrackVO to PerfectItemTrackDO
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param perfectItemTrackVO,perfectItemTrackDO
	 * @return PerfectItemTrackDO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private PerfectItemTrackDO assembleV2D(PerfectItemTrackVO perfectItemTrackVO,PerfectItemTrackDO perfectItemTrackDO){
		if(BlankUtil.isBlank(perfectItemTrackDO)){
			perfectItemTrackDO = new PerfectItemTrackDO();
		}
		BeanUtil.convertVoAsDo(perfectItemTrackVO, perfectItemTrackDO);
		perfectItemTrackDO.setTracker(new HmEmployeeDomain(perfectItemTrackVO.getTrackerId()));
		perfectItemTrackDO.setPerfectItemRequisition(new PerfectItemRequisitionDO(perfectItemTrackVO.getPerfectItemRequisitionId()));
		return perfectItemTrackDO;
	}
	/**
	 * 方法描述:DOlist to VOlist
	 * @author 胡煦
	 * Create on:2008-12-30
	 * @param DOlist
	 * @return List<PerfectItemTrackVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private List<PerfectItemTrackVO> changelistDOToVO(List<PerfectItemTrackDO> DOlist){
		if(DOlist.size() < 1){
			return null;
		}
		List<PerfectItemTrackVO> VOlist = new ArrayList<PerfectItemTrackVO>();
		for(PerfectItemTrackDO itemTrackDO : DOlist){
			VOlist.add(this.assembleD2V(itemTrackDO, null));
		}
		return VOlist;
	}

	private IPerfectItemTrackDao perfectItemTrackDao;
	private IPerfectItemRequisitionDao perfectItemRequisitionDao;
	/**
	 * @param perfectItemTrackDao the perfectItemTrackDao to set
	 */
	public void setPerfectItemTrackDao(IPerfectItemTrackDao perfectItemTrackDao) {
		this.perfectItemTrackDao = perfectItemTrackDao;
	}

	/**
	 * @param perfectItemRequisitionDao the perfectItemRequisitionDao to set
	 */
	public void setPerfectItemRequisitionDao(
			IPerfectItemRequisitionDao perfectItemRequisitionDao) {
		this.perfectItemRequisitionDao = perfectItemRequisitionDao;
	}
}
